// pages/vip-center/vip-center.ts
import api from "../../api/index";
import common from "../../common/index";
import constants from "../../common/constants";
import { formatTimeToDateStr} from "../../utils/util";

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    navBarHeight: app.globalData.navBarHeight,
    userInfo: app.globalData.user,

    vipInfo: app.globalData.vipInfo,

    storageInfo: {
      use: 0,
      total: 0,
      percent: 0,
    },
    productDescObj: constants.VipRightsDesc,
    orderList: [],
    orderCount: 0,
  },

  handleBack() {
    wx.navigateBack()
  },

  getOrderList() {
    this.data.orderList = []
    api.getOrderList(1, 30).then((res) => {
      if (res.data) {
        this.data.orderCount = res.data.total
        this.data.orderList = res.data.list || []
        this.data.orderList.forEach((item) => {
          try {
            item.price = item.price/100
            if (item.createTime) {
              item.timeStr = formatTimeToDateStr(item.createTime)
            }
          } catch(e){}

        })
        this.setData({
          orderCount: this.data.orderCount,
          orderList: this.data.orderList,
        })
      }
    })
  },

  handlePopBuyVip() {
    wx.navigateTo({
      url: '/pages/vip-buy/vip-buy'
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    wx.showLoading({
      title: '加载中'
    })
    const vipInfo = await common.getVipInfo()
    common.setAppVipInfo(vipInfo, app.globalData)
    this.data.storageInfo = await common.getUserStorageInfo(this.data.userInfo.uid)
    this.setData({
      storageInfo: this.data.storageInfo,
      vipInfo: app.globalData.vipInfo,
    })
    wx.hideLoading()
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})